<?php

class Filter
{
	private $filterList;
	
	public function __construct ()
	{
		$defaultFilter = array(
			"is",
			"te",
			"niet",
			"dat",
			"die",
			"voor",
			"hij",
			"ik",
			"zijn",
			"was",
			"aan",
			"ook",
			"de",
			"het",
			"een",
			"to",
			"en",
			"een",
			"de", 
			"het",
			"van",
			"met",
			"in",
			"kan",
			"op",
			"onder",
			"over",
			"mensen",
			"tijd",
			"man",
			"jaar",
			"plaats",
			"vrouw",
			"leven",
			"dag",
			"aantal",
			"werk",
			"groot",
			"nieuw",
			"goed",
			"laatst",
			"klein",
			"later",
			"lang",
			"heel",
			"hebben",
			"worden",
			"zullen",
			"kunnen",
			"moeten",
			"zeggen",
			"komen",
			"naar",
			"http",
			"inmiddels",
			"deze",
			"bedankt",
			"dank",
			"maar"
		);
		$this->blacklist($defaultFilter);
	}
	
	public function blacklist ($words)
	{
		if (is_array($words))
		{
			foreach ($words as $word)
			{
				$this->addToFilter($word);
			}
		}
		else
		{
			$this->addToFilter($word);
		}
	}
	
	public function whitelist ($words)
	{
		if (is_array($words))
		{
			foreach ($words as $word)
			{
				$this->addToFilter($word, true);
			}
		}
		else
		{
			$this->addToFilter($word, true);
		}
	}
	
	public function applyFilter ($words, $minWordLength = 4)
	{
		$filteredList = array();
		foreach ($words as $word)
		{
			$word = strtolower($word);
			if (isset($this->filterList[$word]))
			{
				// if whitelisted word
				if ($this->filterList[$word])
				{
					$filteredList[] = $word;
				}
			}
			else if (strlen($word) >= $minWordLength)
			{
				$filteredList[] = $word;
			}
		}
		return $filteredList;
	}
	
	private function addToFilter ($word, $keep = false)
	{
		$this->filterList[strtolower($word)] = $keep;
	}
}

?>